home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15806 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.3 KB  |  107 lines

  1. Path: wintermute.ecs.fullerton.edu!titan!grosin
  2. From: grosin@titan (Gil Rosin)
  3. Newsgroups: comp.lang.c++
  4. Subject: How do I put DIFFERENT classes in the same linked list?
  5. Date: 8 Apr 1996 05:44:08 GMT
  6. Organization: California State University at Fullerton
  7. Message-ID: <4ka938$bj6@wintermute.ecs.fullerton.edu>
  8. NNTP-Posting-Host: titan.ecs.fullerton.edu
  9. X-Newsreader: TIN [version 1.2 PL2]
  10.  
  11. I'm trying to write a program where I need to maintain a linked list of
  12. various items. Each item is a class, but each item is different from another
  13. item. Let me give an example to clarify:
  14.  
  15. A library catalog for example, there is a BOOK class and a MAGAZINE class, 
  16. then there is an ITEM class from which BOOK and MAGAZINE are derivided (Tell
  17. me if there is a better way to do this!!). ITEM has a ITEM *next pointer.
  18.  
  19. Now, I want to be able to basically construct a linked list of BOOK and
  20. MAGAZINE classes with out really knowing which one is which.
  21.  
  22. Here is the source code I have worked up so far:
  23.  
  24. #include <iostream.h>
  25.  
  26. class A
  27.  {
  28.   public:
  29.     A *next;
  30.     A *prev;
  31.     void Print();
  32.  };
  33.  
  34. class Demo
  35.  {
  36.   public:
  37.     void Insert(A *Temp);
  38.     void Insert2(A *Temp);
  39.     void Test(void);
  40.     A *First;
  41.  };
  42.  
  43. class B : public A
  44.  {
  45.   void Print();
  46.  };
  47.  
  48. class C : public A
  49.  {
  50.   void Print();
  51.  };
  52.  
  53. main()
  54.  {
  55.   Demo *T = new Demo;
  56.   A *Test = new B;
  57.   T->Insert(Test);
  58.   Test = new C;
  59.   T->Insert2(Test);
  60.   T->Test();
  61.   return 0;
  62.  }
  63.  
  64. void B::Print()
  65.  {
  66.   cout << "I am in class B" << '\n';
  67.  }
  68.  
  69. void C::Print()
  70.  {
  71.   cout << "I am in class C" << '\n';
  72.  }
  73.  
  74. void Demo::Insert(A *Temp)
  75.  {
  76.   First = Temp;
  77.  }
  78.  
  79. void Demo::Insert2(A *Temp)
  80.  {
  81.   First->next = Temp;
  82.  }
  83.  
  84. void Demo::Test(void)
  85.  {
  86.   First->Print();
  87.   First->next->Print();
  88.  }
  89.  
  90. void A::Print()
  91.  {
  92.   cout << "I am in class A" << '\n';
  93.  }
  94.  
  95. Now, as you can see, I want to have a linked list that contains EITHER
  96. B or C Items, but in the void Demo::Test() function, I want to be able to
  97. just call them blindly with out knowing which is which. When I compile this
  98. I get NO errors, but when I run it the two lines that print out are the
  99. class A print function. It should print out the class B and then the class C.
  100.  
  101. How can I do this? I am not worrying about constructors/destructors etc
  102. right now, just worrying about how to get it to work.
  103.  
  104. Thanks. Please reply via email to grosin@titan.fullerton.edu.
  105.  
  106.  
  107.